package com.nest.WorkNest.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.nest.WorkNest.R;

import java.util.ArrayList;
import java.util.HashMap;

public class MSGAdapter extends BaseAdapter {
    private ArrayList<HashMap<String, String>> dataList;
    private Context context;

    public MSGAdapter(Context context,
                      ArrayList<HashMap<String, String>> dataList) {
        this.dataList = dataList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private class ViewHolder {
        TextView title, msgcontent;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.layout_mymsg_item, null);
            holder.title = (TextView) convertView.findViewById(R.id.tv_msg_title);
            holder.msgcontent = (TextView) convertView.findViewById(R.id.tv_msg_content);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        HashMap<String, String> map = dataList.get(position);
        holder.title.setText(map.get("title"));
        holder.msgcontent.setText(map.get("content"));
        return convertView;
    }

    public void update(ArrayList<HashMap<String, String>> dataList) {
        this.dataList = dataList;
        notifyDataSetChanged();
    }
}
